package top.ochiamalu.aioj.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ochiamalu.aioj.common.BaseResponse;
import top.ochiamalu.aioj.common.ErrorCode;
import top.ochiamalu.aioj.domain.dto.email.ModifyEmailDTO;
import top.ochiamalu.aioj.exception.BusinessException;
import top.ochiamalu.aioj.service.EmailService;
import top.ochiamalu.aioj.util.ResultUtils;

import javax.annotation.Resource;

/**
 * 电子邮件控制器
 *
 * @author ochiamalu
 * @date 2025/03/31
 */
@RestController
@RequestMapping("/emails")
public class EmailController {

    @Resource
    private EmailService emailService;

    /**
     * 验证电子邮件
     *
     * @param token 令牌
     * @return boolean
     */
    @GetMapping("/verify/{token}")
    public BaseResponse<Boolean> verifyEmail(@PathVariable String token) {
        return ResultUtils.success(emailService.verifyEmail(token));
    }

    /**
     * 修改主电子邮件
     *
     * @param modifyEmailDTO 修改电子邮件dto
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/primary")
    public BaseResponse<Boolean> modifyPrimaryEmail(@RequestBody ModifyEmailDTO modifyEmailDTO) {
        if (modifyEmailDTO.getId() == null || modifyEmailDTO.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(emailService.modifyPrimaryEmail(modifyEmailDTO.getId()));
    }

    /**
     * 重新发送验证电子邮件
     *
     * @param modifyEmailDTO 修改电子邮件dto
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/resend")
    public BaseResponse<Boolean> resendVerificationEmail(@RequestBody ModifyEmailDTO modifyEmailDTO) {
        if (modifyEmailDTO.getId() == null || modifyEmailDTO.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(emailService.resendVerificationEmail(modifyEmailDTO.getId()));
    }

    /**
     * 删除电子邮件
     *
     * @param modifyEmailDTO 修改电子邮件dto
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteEmail(@RequestBody ModifyEmailDTO modifyEmailDTO) {
        if (modifyEmailDTO.getId() == null || modifyEmailDTO.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(emailService.deleteEmail(modifyEmailDTO.getId()));
    }
}
